// src/services/mockBookService.js
import { Book } from '@/models/Book'
import { BorrowRecord } from '@/models/BorrowRecord'

// Mock数据
const mockBooks = [
  {
    id: 1,
    isbn: '978-0-123456-78-9',
    title: 'Spring Boot实战',
    author: '张三',
    genre: '编程',
    publicationDate: '2023-01-15',
    available: true,
    totalCopies: 5,
    availableCopies: 3
  },
  {
    id: 2,
    isbn: '978-0-987654-32-1',
    title: 'Vue.js从入门到精通',
    author: '李四',
    genre: '编程',
    publicationDate: '2022-11-20',
    available: false,
    totalCopies: 3,
    availableCopies: 0
  },
  {
    id: 3,
    isbn: '978-1-111111-11-1',
    title: '百年孤独',
    author: '加西亚·马尔克斯',
    genre: '文学',
    publicationDate: '2020-05-10',
    available: true,
    totalCopies: 10,
    availableCopies: 8
  }
]

// Mock借阅记录
let mockBorrowRecords = [
  {
    id: 1,
    bookId: 2,
    borrowerName: '王五',
    borrowerPhone: '13800138001',
    borrowDate: '2024-01-15T10:30:00',
    returnDate: null,
    returned: false
  },
  {
    id: 2,
    bookId: 1,
    borrowerName: '赵六',
    borrowerPhone: '13900139002',
    borrowDate: '2024-01-20T14:20:00',
    returnDate: '2024-01-25T09:15:00',
    returned: true
  }
]
let nextRecordId = 3

export const mockBookService = {
  // ... 之前的图书相关方法保持不变 ...
  //增加 图书
  async createBook(bookData) {
    return new Promise((resolve) => {
      setTimeout(() => {
        let nextId;
        const newBook = new Book({
          ...bookData,
          id: nextId++,
          available: bookData.totalCopies > 0,
          availableCopies: bookData.totalCopies
        })
        mockBooks.push(newBook)
        resolve(newBook)
      }, 500)
    })
  },

  // 借阅图书（新增借阅人信息）
  async borrowBook(bookId, borrowerName, borrowerPhone) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const book = mockBooks.find(b => b.id === parseInt(bookId))
        if (!book) {
          reject(new Error('Book not found'))
          return
        }
        if (!book.available || book.availableCopies <= 0) {
          reject(new Error('Book is not available for borrowing'))
          return
        }

        // 创建借阅记录
        const newRecord = new BorrowRecord({
          id: nextRecordId++,
          bookId: parseInt(bookId),
          borrowerName,
          borrowerPhone,
          borrowDate: new Date().toISOString(),
          returnDate: null,
          returned: false
        })
        mockBorrowRecords.push(newRecord)

        // 更新图书库存
        book.availableCopies--
        book.available = book.availableCopies > 0

        resolve(new Book(book))
      }, 500)
    })
  },

  // 归还图书
  async returnBook(recordId) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const record = mockBorrowRecords.find(r => r.id === parseInt(recordId))
        if (!record) {
          reject(new Error('Borrow record not found'))
          return
        }
        if (record.returned) {
          reject(new Error('Book already returned'))
          return
        }

        // 更新借阅记录
        record.returnDate = new Date().toISOString()
        record.returned = true

        // 更新图书库存
        const book = mockBooks.find(b => b.id === record.bookId)
        if (book) {
          book.availableCopies++
          book.available = true
        }

        resolve(new Book(book))
      }, 500)
    })
  },

  // 获取图书的借阅记录
  async getBorrowRecordsByBookId(bookId) {
    return new Promise(resolve => {
      setTimeout(() => {
        const records = mockBorrowRecords
          .filter(r => r.bookId === parseInt(bookId))
          .map(record => new BorrowRecord(record))
        resolve(records)
      }, 300)
    })
  },

  // 获取所有活跃借阅记录
  async getActiveBorrowRecords() {
    return new Promise(resolve => {
      setTimeout(() => {
        const records = mockBorrowRecords
          .filter(r => !r.returned)
          .map(record => new BorrowRecord(record))
        resolve(records)
      }, 300)
    })
  },

  // 获取借阅记录详情
  async getBorrowRecordById(recordId) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const record = mockBorrowRecords.find(r => r.id === parseInt(recordId))
        if (record) {
          resolve(new BorrowRecord(record))
        } else {
          reject(new Error('Borrow record not found'))
        }
      }, 300)
    })
  }
}